package com.kust.smarthome.web.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @RequestMapping("/pay")
    public String back() throws Exception {
        Config config=new Config();
        config.protocol = "https";
        config.signType = "RSA2";
        config.merchantPrivateKey="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCcax8plDjEcp+RFnqmv2deS8+f4l1m4URhAJEz+bQEB2JTyiLQKO7DsVpOek4PbjoeWSD288GaUVW3eN8FDyGccBE2nW1eza4kf0769lM1IPKqXkJW1VxLTA87t5itAzfBViqYKSlLqyw3qG9QrlQ+VofG/WCt7vnjKJSHMbtG8oJFWPJ87XCBj5p4u0LonZIWOjQg1WhEcRZ5D8cJ+Xnj7is/57HJfP5AGHJSsdpFp9PJtOOZ+eVrd698HFUghcqwsmuXnPdH8nqVppClG+EB+D82+1fZGWhu6jxu21whSGNqszoDlF/YSU4Nc560lu4BhMnT3r9k3TI7Hcf4cI5hAgMBAAECggEAQ8aSySRleJGmHGreGFSFSLGWtFOUXxiq1VE2mYvP+ZLtgxrp/MJPutGlLK4IFt2PJ0k9ZGP4dpJZjtc742hMCwnwG3cZRePfWgcjxNBo+3NcUn/nRNLMXAAH9bs213i2dvqMGER1lUkFLtS37wCqlhxOVuXBN3x+IHSb7vyYhkOqPmM1gZ+5K+lVRxtfpwmk/Hzn4IMn+M9jES+ogKcod2ur/wdWh4MzEuqUr9T2O73glsOkOPv9ZaHOYmXGLHsbOc1o3eXa7sYoxvfxbv6LwuuazUEIcoKiazo+4/t15j1qIeDjRYi17+cmjdrdmTPXmtN0g0G0Q9zCr5oYVqWe8QKBgQD7lEK+p+5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ssN6hvUK5UPlaHxv1gre754yiUhzG7RvKCRVjyfO1wgY+aeLtC6J2SFjo0INVoRHEWeQ/HCfl54+4rP+exyXz+QBhyUrHaRafTybTjmfnla3evfBxVIIXKsLJrl5z3R/J6laaQpRvhAfg/NvtX2Rlobuo8bttcIUhjarM6A5Rf2ElODXOetJbuAYTJ096/ZN0yOx3H+HCOYQIDAQAB";
        config.gatewayHost="openapi.alipaydev.com";
        Factory.setOptions(config);
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2021000117686966", config.merchantPrivateKey, "json", "UTF-8", config.alipayPublicKey, "RSA2"); //获得初始化的AlipayClient

        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
//        AlipayTradePagePayResponse response = Factory.Payment
//                .Page()
//                .pay("这是一个商品", "1234",
//                        "100", "www.baidu.com");
//        return response.body;

        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"2021000117686966\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return form;

//        String url = "https://openapi.alipaydev.com/gateway.do";       //蚂蚁测试沙箱地址

//        IAopClient client = new DefaultAopClient(url, APPID, newPrivateKey, "json", "1.0", "RSA", newPublicKey, CHARSET, false);
//        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
//        request.BizContent = "{" +
//                "    \"body\":\"对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。\"," +
//                "    \"subject\":\"大乐透\"," +
//                "    \"out_trade_no\":\"70501111111S033311119\"," +
//                "    \"timeout_express\":\"90m\"," +
//                "    \"total_amount\":9.00," +
//                "    \"product_code\":\"QUICK_WAP_PAY\"" +
//                "  }";
//        AlipayTradeWapPayResponse response = client.pageExecute(request);
//        string form = response.Body;
//        Response.Write(form);
    }

   /* public static String app_id = "2021000117686966";
    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCcax8plDjEcp+RFnqmv2deS8+f4l1m4URhAJEz+bQEB2JTyiLQKO7DsVpOek4PbjoeWSD288GaUVW3eN8FDyGccBE2nW1eza4kf0769lM1IPKqXkJW1VxLTA87t5itAzfBViqYKSlLqyw3qG9QrlQ+VofG/WCt7vnjKJSHMbtG8oJFWPJ87XCBj5p4u0LonZIWOjQg1WhEcRZ5D8cJ+Xnj7is/57HJfP5AGHJSsdpFp9PJtOOZ+eVrd698HFUghcqwsmuXnPdH8nqVppClG+EB+D82+1fZGWhu6jxu21whSGNqszoDlF/YSU4Nc560lu4BhMnT3r9k3TI7Hcf4cI5hAgMBAAECggEAQ8aSySRleJGmHGreGFSFSLGWtFOUXxiq1VE2mYvP+ZLtgxrp/MJPutGlLK4IFt2PJ0k9ZGP4dpJZjtc742hMCwnwG3cZRePfWgcjxNBo+3NcUn/nRNLMXAAH9bs213i2dvqMGER1lUkFLtS37wCqlhxOVuXBN3x+IHSb7vyYhkOqPmM1gZ+5K+lVRxtfpwmk/Hzn4IMn+M9jES+ogKcod2ur/wdWh4MzEuqUr9T2O73glsOkOPv9ZaHOYmXGLHsbOc1o3eXa7sYoxvfxbv6LwuuazUEIcoKiazo+4/t15j1qIeDjRYi17+cmjdrdmTPXmtN0g0G0Q9zCr5oYVqWe8QKBgQD7lEK+p+5EhtKQGKKagnPrZ+z8WFjgTGhK5S0DQCnP7IO1Lh9PRQHh+K1YPb5HfBal0Rimnp7cvxeXnM/QebWIBA+fp/VFLGuFUD98JPDsQOLxTPdTqOKy0xLReyHUXaMG+L0FTMopkTudqqswPv+l0wUPI83GHelUZoC9BR8XxQKBgQCfKsbRJ6me9eSPjPC0kM9mvrpeYbuWQg238l/362Lg3zDzVgqmOxU561wj97W4e16jZ3vyS6/KMYstC/eL2jJbD5RJD+U09IJfnH/b2clVndliR8YIGt/XVdMgJKoI2CASCEvbPrgBpGmTPX4ylLKnQdg8XyU3gLrQfne9NQUp7QKBgGjsk826cihIZqdMbvyr7ba/eFpiambZ1yFtZZGWgBnE1cWJv+sqp7dcJMfJsINV4Ig1pedP/oeodyl3cEDNkgVxVCbNQQEhnpJSPrgDHCwcf8oNxJUyuaHFYHLXvOs6RlURBpygQyyE8u02EgGkNLEvSkvfLeIz9W2K9qRe73HVAoGAUUXOb04JVpnIM2SMJCctQBFzqG3PE1J3Hs4sIrOWsaxFmnr8xRS2KfT7/4Gk0KZuFJHqwDXVdTEHF+FcbZTk0YqK9SOgrgGL8LZAs0npfEY1kfGENO9EDPhN7cPK7ukstwh1c2X7HxuMrRphLgKvlqgQegM7RbP9sChji8Fe+aECgYAnR1X9SsjONzmnK/7zM8/Bhoef1uAkOE8wf1YhcnqRNdZotxzoPHseK5NAEknH5A6aX22FKniDCFrulgySbWJoYnp/nEaW+ifgVHN53TpJpKUcFN0ar3pK9QMOsVo9sh47DU/LbxakoB0OssNRdpLoAE3uRX3NcE54/RD/w/bWxw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnGsfKZQ4xHKfkRZ6pr9nXkvPn+JdZuFEYQCRM/m0BAdiU8oi0Cjuw7FaTnpOD246Hlkg9vPBmlFVt3jfBQ8hnHARNp1tXs2uJH9O+vZTNSDyql5CVtVcS0wPO7eYrQM3wVYqmCkpS6ssN6hvUK5UPlaHxv1gre754yiUhzG7RvKCRVjyfO1wgY+aeLtC6J2SFjo0INVoRHEWeQ/HCfl54+4rP+exyXz+QBhyUrHaRafTybTjmfnla3evfBxVIIXKsLJrl5z3R/J6laaQpRvhAfg/NvtX2Rlobuo8bttcIUhjarM6A5Rf2ElODXOetJbuAYTJ096/ZN0yOx3H+HCOYQIDAQAB";
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";
    // 签名方式
    public static String sign_type = "RSA2";
    // 字符编码格式
    public static String charset = "utf-8";
    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipay.com/gateway.do";
    // 支付宝网关
    public static String log_path = "C:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    *//**
     * 写日志，方便测试（看网站需求，也可以改成把记录存入数据库）
     * @param sWord 要写入日志里的文本内容
     *//*
    @RequestMapping("/pay")
    public static void logResult(String sWord) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/
    
}
